package 整数转罗马数字;

public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();

        int input = 400;

        System.out.println(solution.intToRoman(input));
    }

    /**  MCMXCIV
     * 字符          数值
     * I             1         0
     * V             5         1
     * X             10        2
     * L             50        3
     * C             100       4
     * D             500       5
     * M             1000      6
     *
     * 来源：力扣（LeetCode）
     * 链接：https://leetcode-cn.com/problems/integer-to-roman
     * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
     *
     *
     * @param num
     * @return
     */
    public String intToRoman(int num) {

        StringBuilder result = new StringBuilder();

        int[] numbers = new int[]{1,5,10,50,100,500,1000};
        String[] types = new String[]{"I","V","X","L","C","D","M"};

        int length = 7;

        for (int i = numbers.length - 1; i >= 0; i--) {
            int count = num / numbers[i];

            if(num == 0)
                break;
            if(count == 0 && i >= 5 && (count = (num + 100) / numbers[i]) > 0){
                result.append(types[4]);
                num += numbers[4];
            }

            else if(count == 0 && i >= 3  &&  (count = (num + 10) / numbers[i]) > 0){
                result.append(types[2]);
                num += numbers[2];
            }

            else if(count == 0 && i >= 1 && (count = (num + 1) / numbers[i]) > 0){
                result.append(types[0]);
                num += numbers[0];
            }


            if(count > 0){
                while(count != 0){
                   result.append(types[i]);
                   count--;

                   if(count == 0){
                       num = num % numbers[i];

                       if(num == 0)
                           break;

                       if(count == 0 && i >= 6 && (count = (num + 100) / numbers[i]) > 0){
                           result.append(types[4]);
                           num += numbers[4];
                       }

                       else if(count == 0 && i >= 3  &&  (count = (num + 10) / numbers[i]) > 0){
                           result.append(types[2]);
                           num += numbers[2];
                       }

                       else if(count == 0 && i >= 1 && (count = (num + 1) / numbers[i]) > 0){
                           result.append(types[0]);
                           num += numbers[0];
                       }
                   }
                }
            }
        }

        return result.toString();
    }

    /**
     * 示例 1:
     *
     * 输入: 3
     * 输出: "III"
     * 示例 2:
     *
     * 输入: 4
     * 输出: "IV"
     * 示例 3:
     *
     * 输入: 9
     * 输出: "IX"
     * 示例 4:
     *
     * 输入: 58
     * 输出: "LVIII"
     * 解释: L = 50, V = 5, III = 3.
     * 示例 5:
     *
     * 输入: 1994
     * 输出: "MCMXCIV"
     * 解释: M = 1000, CM = 900, XC = 90, IV = 4.
     *
     * 来源：力扣（LeetCode）
     * 链接：https://leetcode-cn.com/problems/integer-to-roman
     * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
     */

}
